package com.manager.common.job;

import com.manager.common.customer.utils.RemoteExecute;
import com.manager.common.holder.SpringBootContextHolder;
import com.manager.common.quartz.QuartzManager;
import com.manager.common.utils.DataTools;
import com.manager.common.utils.DateTools;
import com.manager.common.vo.ResultVO;
import com.manager.container.service.DockerContainerBackupService;
import com.manager.container.service.DockerDeployService;
import com.manager.model.DockerContainerBackupLogs;
import com.manager.model.DockerDeploy;
import com.manager.model.DockerDeployVolume;
import net.sf.json.JSONArray;
import org.quartz.DisallowConcurrentExecution;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;

import java.util.List;

@DisallowConcurrentExecution
public class IntervalBackupContainerVolume implements Job {

    private DockerDeployService dockerDeployService = SpringBootContextHolder.getBean(DockerDeployService.class);

    private DockerContainerBackupService dockerContainerBackupService = SpringBootContextHolder.getBean(DockerContainerBackupService.class);

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
      try {
          System.out.println("Interval Backup Container Volume Backup");
          DockerDeploy dockerDeploy = (DockerDeploy) context.getJobDetail().getJobDataMap().get(QuartzManager.getInstance().getJobParamsKeyName());
          List<DockerDeployVolume> dockerDeployVolumeList = dockerDeployService.getDockerDeployVolumeList(dockerDeploy);
          if(dockerDeployVolumeList == null || dockerDeployVolumeList.isEmpty()){
              return;
          }
          String saveDockerContainerVolumePath = DataTools.getSystemSettingStringValByKey("docker.container.volume.backup.path");
          String command = "mkdir -p " + saveDockerContainerVolumePath + "/" + dockerDeploy.getContainerID();
          String command2 = "tar -cvzf " + saveDockerContainerVolumePath + "/" + dockerDeploy.getContainerID()
                  + "/" + DateTools.getCurrentDateString("yyyyMMddHHmmssSSS") + ".tar.gz";
          for(DockerDeployVolume dockerDeployVolume : dockerDeployVolumeList){
              command2 +=" " + dockerDeployVolume.getHostPath();
          }
          ResultVO<JSONArray> resultVO = RemoteExecute.executeSsh(command,command2);
          DockerContainerBackupLogs dockerContainerBackupLogs = new DockerContainerBackupLogs();
          dockerContainerBackupLogs.setContainerId(dockerDeploy.getContainerID());
          dockerContainerBackupLogs.setContainerName(dockerDeploy.getContainerID());
          dockerContainerBackupLogs.setDockerDeployId(dockerDeploy.getId());
          dockerContainerBackupLogs.setHasSuccess(resultVO.getCode());
          JSONArray jsonArray = JSONArray.fromObject(resultVO.getData());
          StringBuffer sb = new StringBuffer();
          for(int i=0; i<jsonArray.size(); i++){
              sb.append(jsonArray.getString(i)).append("\n");
          }
          dockerContainerBackupLogs.setLogContents(sb.toString());
          dockerContainerBackupService.addLogs(dockerContainerBackupLogs);
      }catch (Exception e){
          e.printStackTrace();
      }
    }
}
